package com.dominator.controller.api;

import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.utils.JsonUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.service.ApiMachineService;
import com.dominator.service.ApiMerchantService;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.exception.ApiException;
import com.dominator.utils.network.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/api/v1/machine")
public class ApiMachineController {
    @Autowired
    HttpServletRequest request;

    @Autowired
    ApiMachineService apiMachineService;



    /**
     * 机器初始化
     *
     * @return
     */
    @PostMapping(value = "/init")
    public ApiMessage init(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiMachineService.init(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }


    /**
     * 用户退出机器
     *
     * @return
     */
    @PostMapping(value = "/userLogout")
    public ApiMessage userLogout(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiMachineService.userLogout(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }


    /**
     * 查询机器和店铺名称
     *
     * @return
     */
    @PostMapping(value = "/getMachineAndShopById")
    public ApiMessage getMachineAndShopById(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiMachineService.getMachineAndShopById(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }


    /**
     * 下单
     *
     * @return
     */
    @PostMapping(value = "/createOrder")
    public ApiMessage createOrder(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiMachineService.createOrder(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }




    /**
     * 获取加奖结果
     *
     * @return
     */
    @PostMapping(value = "/orderPlusResult")
    public ApiMessage orderPlusResult(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiMachineService.orderPlusResult(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }


    /**
     * 获取机器登录状态
     *
     * @return
     */
    @PostMapping(value = "/getMachineLoginStatus")
    public ApiMessage getMachineLoginStatus(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiMachineService.getMachineLoginStatus(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }


}
